//
//  YKHomeViewController.m
//  YunKang
//
//  Created by leoking870 on 2017/9/24.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKHomeViewController.h"
#import <TFImageButton.h>
#import "YKClientDataManageController.h"
#import "YKClientVisitManageController.h"
#import "YKClientOrderManageController.h"
#import "YKClientDrugPriceManageController.h"
#import "YKApproveListViewController.h"
#import "YKPerformanceManageViewController.h"
@interface YKHomeViewController ()

@end

@implementation YKHomeViewController

- (void)loadView {
    self.view = [[UIScrollView alloc]init];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIImageView* logo = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"index_header_logo"]];
    [logo sizeToFit];
    self.navigationItem.titleView = logo;
    
    UIImageView* topBannerImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"yk_index_banner"]];
    [self.view addSubview:topBannerImageView];
    [topBannerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.trailing.equalTo(self.view);
        make.width.equalTo(self.view.mas_width);
    }];
    
    NSMutableArray* images = [NSMutableArray arrayWithCapacity:6];
    NSArray* titles = @[@"业务管理",@"审批管理",@"业绩管理",@"待办事项"];
    for (int i =1; i <= 4; ++i) {
        NSString* imageName = [NSString stringWithFormat:@"yk_buss_menu_0%zd", i];
        UIImage *image = [UIImage imageNamed:imageName];
        TFImageButton* imageView = [TFImageButton imageButtonWithTitle:titles[i-1] image:image imagePosition:TFImageButtonImagePositionTop];

        [imageView setTitleColor:[UIColor tf_colorWithHexString:@"#8ec21f"] forState:UIControlStateNormal];
        [imageView setTitleImageOffset:8.0f];
        [imageView setContentEdgeInsets:UIEdgeInsetsMake(10, 0, 10, 0)];
        imageView.titleLabel.font = [UIFont systemFontOfSize:12];
        [images addObject:imageView];
        imageView.tag = i;
        [imageView addTarget:self action:@selector(onButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:imageView];
    }
    [images mas_alignHorizontallyWithLineNumber:2 leadSpace:0 space:.5f trailSpace:0 topTo:topBannerImageView.mas_bottom topSpace:0 bottomTo:nil bottomSpace:0 verticalSpace:.5f separatorColor:[UIColor tf_colorWithHexString:@"#e8e8e8"] separatorWidth:.5f];
    UIImageView* imageView = images.lastObject;
    [images removeAllObjects];
    for (int i = 5; i <= 6; ++i) {
        NSString* imageName = [NSString stringWithFormat:@"index_menu_item_%zd", i];
        UIImage *image = [UIImage imageNamed:imageName];
        UIImageView* imageView = [[UIImageView alloc]initWithImage:image];
        [images addObject:imageView];
        imageView.tag = i;
        imageView.userInteractionEnabled = YES;
        UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onImageTapped:)];
        [imageView addGestureRecognizer:gesture];
        [self.view addSubview:imageView];
    }
     [images mas_alignHorizontallyWithLineNumber:2 leadSpace:0 space:.5f trailSpace:0 topTo:imageView.mas_bottom topSpace:.5 bottomTo:self.view.mas_bottom bottomSpace:0 verticalSpace:.5f separatorColor:[UIColor tf_colorWithHexString:@"#e8e8e8"] separatorWidth:.5f];
}

- (void)onButtonTapped:(id)sender {
    TFImageButton *button = sender;
    switch (button.tag) {
        case 1:
        {
            self.tabBarController.selectedIndex = 0;
//            YKClientDataManageController *vc = [[YKClientDataManageController alloc]init];
//            vc.hidesBottomBarWhenPushed = YES;
//            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 2:
        {
            self.tabBarController.selectedIndex = 1;
//            YKClientVisitManageController *vc = [[YKClientVisitManageController alloc]init];
//            vc.hidesBottomBarWhenPushed = YES;
//            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 3:
        {
            YKPerformanceManageViewController *vc = [[YKPerformanceManageViewController alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 4:
        {
//            YKClientDrugPriceManageController *vc = [[YKClientDrugPriceManageController alloc]init];
//            vc.hidesBottomBarWhenPushed = YES;
//            [self.navigationController pushViewController:vc animated:YES];
            YKApproveListViewController *vc = [[YKApproveListViewController alloc]init];
            vc.title = @"待办事项";
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        default:
            break;
    }
}

- (void)onImageTapped:(UITapGestureRecognizer*)gesture {
    
    switch (gesture.view.tag) {
        case 1:
        {
            YKClientDataManageController *vc = [[YKClientDataManageController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 2:
        {
            YKClientVisitManageController *vc = [[YKClientVisitManageController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
        default:
            break;
    }
}

@end
